import re

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from pyquery import PyQuery as pq
import os
import urllib.request

# browser = webdriver.PhantomJS()
# browser.set_window_size(1400, 900)

browser = webdriver.Chrome()
wait = WebDriverWait(browser, 10)
print('所有链接获取：长裤输入0 七分裤输入1 五分裤输入2 短裤输入3 七分裤／九分裤输入4')
href = input()
if href == "0":
    href = "https://s.taobao.com/search?q=%E8%A3%A4%E5%AD%90+%E5%A5%B3&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306&cps=yes&ppath=122276111%3A20525"
    KEYWORD = '长裤链接'
if href == "1":
    href = "https://s.taobao.com/search?q=%E8%A3%A4%E5%AD%90+%E5%A5%B3&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306&cps=yes&ppath=122276111%3A30271"
    KEYWORD = '七分裤链接'
if href == "2":
    href = "https://s.taobao.com/search?q=%E8%A3%A4%E5%AD%90+%E5%A5%B3&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306&cps=yes&ppath=122276111%3A30272"
    KEYWORD = '五分裤链接'
if href == "3":
    href = "https://s.taobao.com/search?q=%E8%A3%A4%E5%AD%90+%E5%A5%B3&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306&cps=yes&ppath=122276111%3A20524"
    KEYWORD = '短裤链接'
if href == "4":
    href = "https://s.taobao.com/search?q=%E8%A3%A4%E5%AD%90+%E5%A5%B3&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306&cps=yes&ppath=122276111%3A72202018"
    KEYWORD = '七分裤九分裤链接'

def search():
    print('正在搜索'+ KEYWORD + "文本链接")
    try:
        browser.get(href)
        total = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#mainsrp-pager > div > div > div > div.total')))

        # all_links = browser.find_elements_by_xpath("./*//a[@data-nid]")
        # print(all_links)
        get_products()
        return total.text
    except Exception as e:
        print(e)
        search()

def next_page(page_number):
    print('正在翻页',page_number)
    try:
        input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#mainsrp-pager > div > div > div > div.form > input')))
        submit = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#mainsrp-pager > div > div > div > div.form > span.btn.J_Submit')))
        input.clear()
        input.send_keys(page_number)
        submit.click()
        wait.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR, '#mainsrp-pager > div > div > div > ul > li.item.active > span'), str(page_number)))
        get_products()
    except TimeoutException:
        next_page(page_number)

def get_products():
    all_links = browser.find_elements_by_xpath("./*//a[@data-nid]")
    with open(KEYWORD+'.txt', 'w', encoding='utf-8') as f:
        for link in all_links:
            new_link = link.get_attribute('href')
            f.write(new_link+'\n')
    f.close()

def main():
    try:
        search()
    except Exception as e:
        print(e)
    finally:
        browser.close()

if __name__ == '__main__':
    main()